package com.luoxiang.socialandroid.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.bean.blbean;

import java.util.List;

public class BlackAdapter extends RecyclerView.Adapter<BlackAdapter.ViewHolder>{
    private List<blbean> list;
    private String url = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/";
    boolean hasVisible = false;
    public BlackAdapter(List<blbean> list) {
        this.list = list;
    }
    public void setData(List<blbean> list) {
        this.list = list;
    }
    public void setJieChuValue(boolean hasVisible){
        this.hasVisible = hasVisible;
    }
    public static interface OnAddClickListener {
        // true add; false cancel
        void onItemClicks(int position); //传递boolean类型数据给activity
    }

    OnAddClickListener onItemAddClick;

    public void setOnAddClickListener(OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_black, parent, false);
        BlackAdapter.ViewHolder holder = new BlackAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView")int position) {
        blbean bean = list.get(position);
        Glide.with(MyAppliaction.getContext())
                .load(url + bean.getIcon())
                .transform(new CircleCrop())
                .into(holder.im_black);
        holder.tx_black.setText(bean.getName());
        if(hasVisible) {
            //true 显示控件
            holder.jiechu.setVisibility(View.VISIBLE);
        }else{
            holder.jiechu.setVisibility(View.GONE);
        }
        holder.jiechu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemAddClick.onItemClicks(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        try {
            return list.size();
        }catch (Exception e){
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView im_black,jiechu;
        private TextView tx_black;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            im_black = itemView.findViewById(R.id.im_black);
            tx_black = itemView.findViewById(R.id.tx_black);
            jiechu = itemView.findViewById(R.id.jiechu);
        }
    }
}
